/*
 * Copyright (C) 2005-2011 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 */

package org.alfresco.demo.jmx;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authority.AuthorityDAO;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthorityService;

/**
 * This class contains the behavior to be attached to the 'cm:person' type for assign
 * configured groups according to user email.
 * 
 * @author Rui Fernandes
 */
public class AssignUserEmailGroup {

	private Map<String, String> emailGroupMap;
	private NodeService nodeService;
	private AuthorityDAO authorityDAO;
	private AuthorityService authorityService;
	private String emailGroups = "";

	public AssignUserEmailGroup(NodeService nodeService,
			AuthorityDAO authorityDAO, AuthorityService authorityService) {
		this.nodeService = nodeService;
		this.authorityDAO = authorityDAO;
		this.authorityService = authorityService;
	}

	public String getEmailGroups() {
		return emailGroups;
	}

	public void setEmailGroups(String value) {
		emailGroups = value;
		initEmailGroupMap(value);

	}

	public String configuredGroupName(String userEmail) {
		if (emailGroupMap == null)
			throw new IllegalStateException("No mail-group map set.");
		for (Entry<String, String> entry : emailGroupMap.entrySet()) {
			if (userEmail.endsWith(entry.getKey())) {
				return entry.getValue();
			}
		}
		return null;
	}

	private void initEmailGroupMap(String emailGroups) {
		Map<String, String> map = new HashMap<String, String>();
		String[] entries = emailGroups.split(",");
		for (String entry : entries) {
			String[] keyVal = entry.split(":");
			map.put(keyVal[0], keyVal[1]);
		}
		emailGroupMap = map;
	}

	private String fetchGroupAuthorityName(String userEmail) {
		String groupName = configuredGroupName(userEmail);
		if (groupName == null)
			return null;
		NodeRef parentGroup = authorityDAO.getAuthorityNodeRefOrNull("GROUP_"
				+ groupName);
		return (String) nodeService.getProperty(parentGroup,
				ContentModel.PROP_AUTHORITY_NAME);
	}

	protected void assignGroup(NodeRef personNode) {
		String email = (String) nodeService.getProperty(personNode,
				ContentModel.PROP_EMAIL);
		String groupName = fetchGroupAuthorityName(email);
		if (groupName != null) {
			String userName = (String) nodeService.getProperty(personNode,
					ContentModel.PROP_USERNAME);
			authorityService.addAuthority(groupName, userName);
		}
	}

}
